package com.example.testrxjava.fragment.system;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.testrxjava.R;
import com.example.testrxjava.activity.search.SearchActivity;
import com.example.testrxjava.activity.systemactivity.SystemTreeActivity;
import com.example.testrxjava.adapter.KnowLedgeTreeAdapter;
import com.example.testrxjava.base.BaseFragment;
import com.example.testrxjava.util.Action;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 项目名称：TestRxjava
 *
 * @Author 郭迎辉
 * Create by 10:16 on 2019/4/18.
 * Describe:知识体系
 */
public class KnowledgeFragment extends BaseFragment {
    @BindView(R.id.system_Recy)
    RecyclerView system_Recy;

    @BindView(R.id.searchTv)
    EditText mSearchTv;

    @BindView(R.id.searchIv)
    ImageView mSearchIv;

    private List<KnowLedgeBean.DataBean> data;
    private KnowLedgeTreeAdapter adapter;
    @Override
    protected void initData() {
        Action.getInstance().getKnowLedgeTree(new KnowLedgeSucessCallback() {
            @Override
            public void systemSucess(KnowLedgeBean bean) {
                if(bean==null){
                    return;
                }
                data = bean.getData();
//                for (int i = 0; i < bean.getData().size(); i++) {
//                    Log.e("====name",bean.getData().get(i).getName());
//                    for (int j = 0; j <bean.getData().get(i).getChildren().size() ; j++) {
//                        Log.e("====children",bean.getData().get(i).getChildren().get(j).getName());
//                    }
//                }
                system_Recy.setLayoutManager(new LinearLayoutManager(getActivity()));
                adapter=new KnowLedgeTreeAdapter(R.layout.item_system_list,data);
                adapter.openLoadAnimation();
                system_Recy.setAdapter(adapter);

                adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        Intent intent=new Intent(getActivity(), SystemTreeActivity.class);
                        Bundle bundle=new Bundle();
                        bundle.putSerializable("bean",data.get(position));
                        Log.e("====点击name",data.get(position).getName());
                        intent.putExtras(bundle);
                        getActivity().startActivity(intent);
                    }
                });

            }

            @Override
            public void systemFail(String message) {

            }
        });

        mSearchTv.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View view) {
                toSearchActivity();
            }
        });

    }

    private void toSearchActivity() {
        Intent intent=new Intent(getActivity(), SearchActivity.class);
        ActivityOptionsCompat compat=ActivityOptionsCompat.makeCustomAnimation(getActivity(),R.anim.anim_animo_alph_open,R.anim.anim_animo_alph_close);
        startActivity(intent, compat.toBundle());

    }

    @Override
    protected View initView() {
        View view= LayoutInflater.from(getActivity()).inflate(R.layout.knowledge_fragment,null);
        data=new ArrayList<KnowLedgeBean.DataBean>();
        return view;
    }
}
